<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="个人总结" prop="slogan">
      <el-input v-model="dataForm.slogan" placeholder="个人总结"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="站点链接：" prop="domain">
      <el-input v-model="dataForm.domain" placeholder="站点链接："></el-input>
    </el-form-item>
    <el-form-item label="版权" prop="notice">
      <el-input v-model="dataForm.notice" placeholder="版权"></el-input>
    </el-form-item>
    <el-form-item label="简短描述" prop="descs">
      <el-input v-model="dataForm.descs" placeholder="简短描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          avatar: 0,
          slogan: '',
          name: '',
          domain: '',
          notice: '',
          descs: '',
          messageId: ''
        },
        dataRule: {
          slogan: [
            { required: true, message: '个人总结不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          domain: [
            { required: true, message: '站点链接：不能为空', trigger: 'blur' }
          ],
          notice: [
            { required: true, message: '版权不能为空', trigger: 'blur' }
          ],
          descs: [
            { required: true, message: '简短描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/mybog/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.slogan = data.user.slogan
                this.dataForm.name = data.user.name
                this.dataForm.domain = data.user.domain
                this.dataForm.notice = data.user.notice
                this.dataForm.descs = data.user.descs
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/mybog/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id':this.dataForm.id,
                'avatar': this.dataForm.avatar || undefined,
                'slogan': this.dataForm.slogan,
                'name': this.dataForm.name,
                'domain': this.dataForm.domain,
                'notice': this.dataForm.notice,
                'descs': this.dataForm.descs,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
